package com.vegetable.modules.service.inbound.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.modules.entity.baseData.ProductSpec;
import com.vegetable.modules.entity.inbound.AgentDeliveryDetail;
import com.vegetable.modules.mapper.baseData.ProductSpecMapper;
import com.vegetable.modules.mapper.inbound.AgentDeliveryDetailMapper;
import com.vegetable.modules.service.inbound.IAgentDeliveryDetailService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 入库-代办交货商品表 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-07
 */
@Slf4j
@Service
@AllArgsConstructor
public class AgentDeliveryDetailServiceImpl extends ServiceImpl<AgentDeliveryDetailMapper, AgentDeliveryDetail> implements IAgentDeliveryDetailService {
    @Autowired
    private ProductSpecMapper productSpecMapper;
    @Override
    public int deleteByOrderId(Long orderId) {
        QueryWrapper<AgentDeliveryDetail> detailQueryWrapper = new QueryWrapper();
        detailQueryWrapper.eq("delivery_id", orderId);
        return this.getBaseMapper().delete(detailQueryWrapper);
    }

    @Override
    public int deleteById(Long detailId) {
        return this.getBaseMapper().deleteById(detailId);
    }

    @Override
    public List<AgentDeliveryDetail> selectByOrderId(Long orderId) {
        QueryWrapper<AgentDeliveryDetail> detailQuery = new QueryWrapper<>();
        detailQuery.eq("delivery_id", orderId);
        detailQuery.orderByAsc("sort_order", "id");
        return this.getBaseMapper().selectList(detailQuery);
    }

    @Override
    public List<AgentDeliveryDetail> selectByOrderIds(List<Long> orderIds) {
        QueryWrapper<AgentDeliveryDetail> detailQuery = new QueryWrapper<>();
        detailQuery.in("delivery_id", orderIds);
        return this.getBaseMapper().selectList(detailQuery);
    }

    @Override
    public List<AgentDeliveryDetail> canPaymentList() {
        return null;
    }

    @Override
    public AgentDeliveryDetail selectByOrderIdAndCommodityIdAndSpecsId(Long orderId, Long commodityId, Long specsId, Integer initNum) {
        QueryWrapper<AgentDeliveryDetail> detailQuery = new QueryWrapper();
        detailQuery.eq("delivery_id", orderId);
        detailQuery.eq("commodity_id", commodityId);
        detailQuery.eq("number", initNum);
        if(ObjectUtil.isNull(specsId)){
            detailQuery.isNull("specs_id");
        }else {
            detailQuery.eq("specs_id", specsId);
        }
        return this.getBaseMapper().selectOne(detailQuery);
    }

    @Override
    public void checkSpecsName() {
        // 1 查询出代办订单明细中 没有规格的数据
        // 2 按照商品id查询规格表，确认是不是真的没数据
        // 3 找出错误的数据，更新
        QueryWrapper<AgentDeliveryDetail> noSpecs = new QueryWrapper();
        noSpecs.isNull("specs_id");
        List<AgentDeliveryDetail> details = baseMapper.selectList(noSpecs);
        for(AgentDeliveryDetail detail : details){
            Long commodityId = detail.getCommodityId();
            QueryWrapper<ProductSpec> specsQuery = new QueryWrapper();
            specsQuery.eq("producer_id", commodityId);
            List<ProductSpec> specList = productSpecMapper.selectList(specsQuery);
            if(CollectionUtil.isNotEmpty(specList)){
                log.info("{}:{}:{}:{}公斤，确实有规格--{}", detail.getId(), detail.getCommodityId(), detail.getCommodityName(), detail.getStandard(), JSONArray.toJSONString(specList));
//                AgentDeliveryDetail updater = new AgentDeliveryDetail();
                detail.setSpecsName(specList.get(0).getName());
                detail.setSpecsId(specList.get(0).getId());
//                updater.setId(detail.getId());
                baseMapper.updateById(detail);
            }else{
//                log.info("{}:{}确实没有规格", detail.getCommodityId(), detail.getCommodityName());
                continue;
            }

        }
    }
}
